package com.q.tqb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.q.tqb.R;
import com.q.tqb.model.MineRedInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.SPUtil;
import com.q.tqb.view.CircleImageView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MineSendRedAdapter extends RecyclerView.Adapter<MineSendRedAdapter.ViewHolder> {
    private ArrayList<MineRedInfo> mRedPersonDatas;
    private Context mContext;
    private String head;
    private int type;


    public MineSendRedAdapter(Context context, ArrayList<MineRedInfo> mRedPersonDatas, int type, String header) {

        this.mRedPersonDatas = mRedPersonDatas;
        this.mContext = context;
        this.type = type;
        if (type == 1) {
            this.head = SPUtil.getString(context, "header");
        } else {
            this.head = header;
        }


    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mine_red, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final MineRedInfo mineRedInfo = mRedPersonDatas.get(position);


        holder.mTvTitle.setText(mineRedInfo.getPacket_title());
        holder.mTvTime.setText(mineRedInfo.getCreate_time());
        if (mineRedInfo.getGet_num() == mineRedInfo.getPacket_num()) {
            holder.mTvMoney.setText("已领完");
            holder.mTvAmount.setVisibility(View.GONE);
            holder.itemView.setOnLongClickListener(null);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onClick(position);
                }
            });
        } else {
            if (type == 1) {
                holder.mTvAmount.setVisibility(View.VISIBLE);
                holder.mTvAmount.setText("数量:" + mineRedInfo.getGet_num() + "/" + mineRedInfo.getPacket_num());
                holder.mTvMoney.setText("余额：" + mineRedInfo.getSurplus_money() + "元");
            } else {
                holder.mTvAmount.setVisibility(View.GONE);
                holder.mTvMoney.setText("数量:" + mineRedInfo.getGet_num() + "/" + mineRedInfo.getPacket_num());
            }
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    mOnItemClickListener.onLongClick(position);
                    return false;
                }
            });
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (type == 1) {
                    mOnItemClickListener.onClick(position);
                } else {
                    if (mineRedInfo.getIs_show_list() == 1) {
                        mOnItemClickListener.onClick(position);
                    } else {
                        Toast.makeText(mContext, "该红包为私发红包，您也可以试一试发给您的好友", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });


        Glide
                .with(mContext)
                .

                        load(head)
                .

                        error(R.drawable.ic_head)
                .

                        placeholder(R.drawable.ic_head)
                .

                        dontAnimate()
                .

                        into(holder.mIvHead);

        //  mineRedInfo.get

    }

    @Override
    public int getItemCount() {
        return mRedPersonDatas.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_money)
        TextView mTvMoney;
        @BindView(R.id.tv_title)
        TextView mTvTitle;
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.tv_amount)
        TextView mTvAmount;
        @BindView(R.id.iv_head)
        CircleImageView mIvHead;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);

        void onLongClick(int position);
    }

    private OnItemClickListener mOnItemClickListener;


    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

}